﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Res = WasaWasa.Properties.Resources;
using System.Net;
using System.Threading;

namespace WasaWasa
{
    partial class MainWindow
    {
        private const string ApiPathCreateFavorite = "/favorites/create/{0}.json";
        private const string ApiPathDestroyFavorite = "/favorites/destroy/{0}.json";

        private void CreateFavorite(string rid)
        {
            AppendLogText(rid + Res.To + Res.MessageCreatingFavorite);

            UriBuilder ub = new UriBuilder(Uri.UriSchemeHttp, ApiHost, ApiPort,
                string.Format(ApiPathCreateFavorite, rid));

            HttpWebRequest wreq = WebRequest.Create(ub.Uri) as HttpWebRequest;
            wreq.Method = WebRequestMethods.Http.Post;
            if (!SetWebRequestOptions(wreq)) return;
            IAsyncResult oar = wreq.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult ar)
            {
                try
                {
                    using (HttpWebResponse wres = wreq.EndGetResponse(ar) as HttpWebResponse)
                    {
                        AppendLogText(rid + Res.To + Res.OperationCreateFavorite + Enum.GetName(typeof(HttpStatusCode), wres.StatusCode));
                    }
                }
                catch (WebException ex)
                {
                    AppendLogText(rid + Res.To + Res.OperationCreateFavorite + Enum.GetName(typeof(HttpStatusCode), (ex.Response as HttpWebResponse).StatusCode));
                }
            }), null);
            ThreadPool.RegisterWaitForSingleObject(oar.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), new object[] { wreq, rid + Res.To + Res.OperationCreateFavorite }, 30000, true);
        }

        private void DestroyFavorite(string rid)
        {
            AppendLogText(rid + Res.To + Res.MessageDestroyingFavorite);

            UriBuilder ub = new UriBuilder(Uri.UriSchemeHttp, ApiHost, ApiPort,
                string.Format(ApiPathDestroyFavorite, rid));

            HttpWebRequest wreq = WebRequest.Create(ub.Uri) as HttpWebRequest;
            wreq.Method = WebRequestMethods.Http.Post;
            if (!SetWebRequestOptions(wreq)) return;
            IAsyncResult oar = wreq.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult ar)
            {
                try
                {
                    using (HttpWebResponse wres = wreq.EndGetResponse(ar) as HttpWebResponse)
                    {
                        AppendLogText(rid + Res.To + Res.OperationDestroyFavorite + Enum.GetName(typeof(HttpStatusCode), wres.StatusCode));
                    }
                }
                catch (WebException ex)
                {
                    AppendLogText(rid + Res.To + Res.OperationDestroyFavorite + Enum.GetName(typeof(HttpStatusCode), (ex.Response as HttpWebResponse).StatusCode));
                }
            }), null);
            ThreadPool.RegisterWaitForSingleObject(oar.AsyncWaitHandle, new WaitOrTimerCallback(TimeOutCallback), new object[] { wreq, rid + Res.To + Res.OperationDestroyFavorite }, 30000, true);
        }
    }
}
